//
//  MainTabBarViewController.m
//  CarCredit
//
//  Created by WDL on 16/12/19.
//  Copyright © 2016年 CarCredit. All rights reserved.
//

#import "MainTabBarViewController.h"

#import "BaseNavigationController.h"
#import "AppDelegate.h"

#define APPLICATION  (AppDelegate *)[UIApplication sharedApplication].delegate


@interface MainTabBarViewController ()

@end

@implementation MainTabBarViewController

/**
 *  系统在第一次使用这个类的时候调用(1个类只会调用一次)--合适的代码放在合适的地方。=
 */
+(void)initialize{
    
    UITabBar *tabBar = [UITabBar appearance];
    
    
    tabBar.backgroundImage = [UIImage imageNamed:@"tab_bg_imgs"];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{
                                                        NSForegroundColorAttributeName : RGBCOLOR(102, 102, 102),
                                                        NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:10.0]
                                                        }forState:UIControlStateNormal];
    
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{
                                                        NSForegroundColorAttributeName:RGBCOLOR(75, 152, 244),
                                                        NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:10.0]
                                                        }forState:UIControlStateSelected];
    
    if([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        
        tabBar.translucent = NO;
    }
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate *app = APPLICATION;
    app.mainTabbarCon = self;
    //添加控制器
    /** 初始化 TabBarItems */
    [self addSubControllersWithSBName:@"Home" withTabItemImageName:@"Home_top" withTabItemSelectedImage:@"Home_After" withTabItemTitleName:@"首页" withTabIndexTag:0];
    
    [self addSubControllersWithSBName:@"Nearby" withTabItemImageName:@"near_top" withTabItemSelectedImage:@"near_After" withTabItemTitleName:@"添加" withTabIndexTag:1];
    
    //资产
    [self addSubControllersWithSBName:@"MyCenter" withTabItemImageName:@"us_top" withTabItemSelectedImage:@"us_After" withTabItemTitleName:@"我的" withTabIndexTag:2];

    
}
/** 用户点击 tabBar的时候会调用这个方法 */
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSLog(@"----tabar did Select 。。%ld",item.tag);
    if (item.tag == 1) {
        BaseNavigationController *rootVc =  self.childViewControllers[1];
        if (rootVc.viewControllers.count>1) {
            [rootVc popToRootViewControllerAnimated:NO];
        }
    }
}
/**
 *  添加 TabBar的 自控制器
 *
 *  @param sbName           要加载的  StoryBoard 的名字
 *  @param image             tabBar上正常显示的Image
 *  @param selectedImageName tabBar上显示的高亮Image，如果不给值 系统将 normal的自动渲染为 蓝色的
 *  @param title                tabBar 图片上要显示的 title 的名字。
 */
-(void)addSubControllersWithSBName:(NSString *)sbName withTabItemImageName:(NSString *)image withTabItemSelectedImage:(NSString *)selectedImageName withTabItemTitleName:(NSString*)title withTabIndexTag:(NSInteger )tag{
    
    /** 加载 StoryBoard */
    UIStoryboard  *tabSb = [UIStoryboard storyboardWithName:sbName bundle:nil];
    /** 从StoryBoard 初始化   Initial(箭头所指的)控制器 */
    UIViewController *tabVC = [tabSb instantiateInitialViewController];
    /**图片下方的 文字*/
    tabVC.tabBarItem.title = title;
    //渲染模式 RenderingMode
    tabVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //
    tabVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //记录tag
    tabVC.tabBarItem.tag = tag;
    /** 添加到 tabBar 的子控制器中 */
    [self addChildViewController:tabVC];
}


@end
